﻿using System.Collections.Generic;
using System.Linq;
using TPOJ.IDAL;
using TPOJ.Model;
using System.Data;

namespace TPOJ.LINQDAL
{
    public class ProblemDAO : IProblemDAO
    {
        public Problem GetProblem(int id)
        {
            Problem result;
            using (var entities = new TPOJEntities())
            {
                var query = from problem in entities.Problems
                            where problem.UniqueID == id
                            select problem;
                result = query.First();
            }
            return result;
        }

        public IList<Problem> GetAllProblems()
        {
            IList<Problem> result = new List<Problem>();

            using (var entities = new TPOJEntities())
            {
                var query = from problem in entities.Problems
                            select problem;
                foreach (var p in query)
                {
                    result.Add(p);
                }
            }
            return result;
        }

        public IList<Problem> GetProblemsByVolume(int volumeId)
        {
            int pid = volumeId * 1000;
            IList<Problem> result = new List<Problem>();

            using (var entities = new TPOJEntities())
            {
                var query = from problem in entities.Problems
                            where problem.UniqueID >= pid && problem.UniqueID < pid + 100
                            orderby problem.UniqueID
                            select problem;
                foreach (var p in query)
                {
                    result.Add(p);
                }
            }
            return result;
        }


        public void Create(Problem problem)
        {
            using (var entities = new TPOJEntities())
            {
                entities.AddToProblems(problem);
                entities.SaveChanges();
            }
        }


        public Problem Edit(Problem problem)
        {
            Problem result;
            using (var entities = new TPOJEntities())
            {
                var query = from p in entities.Problems
                            where p.UniqueID == problem.UniqueID
                            select p;
                result = query.FirstOrDefault();

                result.Title = problem.Title;
                result.Description = problem.Description;
                result.Input = problem.Input;
                result.Output = problem.Output;
                result.SampleInput = problem.SampleInput;
                result.SampleOutput = problem.SampleOutput;
                result.Source = problem.Source;
                result.Hint = problem.Hint;
                result.Date = problem.Date;
                result.Summit = problem.Summit;
                result.Solved = problem.Solved;
                result.TimeLimit = problem.TimeLimit;
                result.MemoryLimit = problem.MemoryLimit;
                result.IsBlocked = problem.IsBlocked;

                try
                {
                    entities.SaveChanges();
                }
                catch (OptimisticConcurrencyException)
                {
                    entities.Refresh(System.Data.Objects.RefreshMode.ClientWins, result);
                    entities.SaveChanges();
                }
            }
            return result;
        }
    }
}
